import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-03-04
 * Time: 14:02
 */
public class test {
    public int majorityElement(int[] nums) {
        Map<Integer,Integer> hash = countNums(nums);

        Map.Entry<Integer,Integer> majorityEntry = null;
        for(Map.Entry<Integer,Integer> entry:hash.entrySet()){
            if(majorityEntry==null || entry.getValue()>majorityEntry.getValue()){
                majorityEntry = entry;
            }
        }
        return majorityEntry.getKey();
    }

    public Map<Integer,Integer> countNums(int[] nums){
        Map<Integer,Integer> counts = new HashMap<>();
        for(int num:nums){
            if(!counts.containsKey(num)){
                counts.put(num,1);
            }else{
                counts.put(num,counts.get(num)+1);
            }
        }
        return counts;
    }
}
